<?php

class KiemKeController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }
   
        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];
 
            $result = $this->deleteKiemke($values);
           
            
            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                 echo'OK';
            
            }
             exit();
        } else if ($action == 'add') {
            $BEP_ID=$_POST['BEP_ID'];
            $KK_NGAY_KIEM_KE_BEGIN=$_POST['KK_NGAY_KIEM_KE_BEGIN'];
            $KK_NGAY_KIEM_KE_END=$_POST['KK_NGAY_KIEM_KE_END'];
            $KK_TONG_KHO=$_POST['KK_TONG_KHO'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
            $result = $this->addKiemke($BEP_ID, $KK_NGAY_KIEM_KE_BEGIN,$KK_NGAY_KIEM_KE_END,$KK_TONG_KHO,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                }
            }
             exit();
             
            
        } else if ($action == 'edit' ) {

            $KK_ID=$_POST['KK_ID'];
            $BEP_ID=$_POST['BEP_ID'];
            $KK_NGAY_KIEM_KE_BEGIN=$_POST['KK_NGAY_KIEM_KE_BEGIN'];
            $KK_NGAY_KIEM_KE_END=$_POST['KK_NGAY_KIEM_KE_END'];
            $KK_TONG_KHO=$_POST['KK_TONG_KHO'];
            $DON_GIA=$_POST['DON_GIA'];
            $SOLUONG=$_POST['SOLUONG'];
            $THANHTIEN=$_POST['THANHTIEN'];
            $ID_HH=$_POST['ID_HH'];
            $result1 = $this->deleteChiTietKiemke($KK_ID);
            $result = $this->updateKiemke($KK_ID,$BEP_ID, $KK_NGAY_KIEM_KE_BEGIN,$KK_NGAY_KIEM_KE_END,$KK_TONG_KHO,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                }
            }
             exit();
            
            

            
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_kiemke' . DS . 'views' . DS . 'kiemke_add.php');
            
            $beps=$this->getAllBep();
            
              if ($beps != null) {
                 
                foreach ($beps as $bep) {

                     
                     $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep');
                    
                }
            }
           $hanghoas = $this->getAllHangHoa();


            if ($hanghoas != null) {
                 
                foreach ($hanghoas as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                   
                    
                    
                    
                    $xtemplate->parse('main.hanghoa');
                    
                }
            }
            
            $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {
                 
                foreach ($hanghoas_gia_soluongs as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    
                    
                    
                    
                    $xtemplate->parse('main.hanghoas_gia_soluong');
                    
                }
            }

            
             $xtemplate->assign('DATE', Utility::getCurrentDate());
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idkiemke'];
            $xtemplate = new XTemplate('./components' . DS . 'com_logic_kiemke' . DS . 'views' . DS . 'kiemke_edit.php');
            $kiemke = $this->getKiemkeById($id);
//            Utility::messageBox($id);

            if ($kiemke != null) {
                
 
                    $xtemplate->assign('KK_ID', $kiemke->getKK_ID());
                    $xtemplate->assign('BEP_ID', $kiemke->getBEP_ID());
                    $xtemplate->assign('KK_NGAY_KIEM_KE_BEGIN', $kiemke->getKK_NGAY_KIEM_KE_BEGIN());
                    $xtemplate->assign('KK_NGAY_KIEM_KE_END', $kiemke->getKK_NGAY_KIEM_KE_END());
                    $xtemplate->assign('KK_TONG_KHO', $kiemke->getKK_TONG_KHO());
                     $beps=$this->getAllBep();
             $option_bep='';
              if ($beps != null) {
                 
                foreach ($beps as $bep) {

                     
                      if($kiemke->getBEP_ID()==$bep->getBEP_ID()){
                          $option_bep .="<option selected=selected value='".$bep->getBEP_ID()."'>".$bep->getBEP_TEN_BEP()."</option>";
                          }else{
                             $option_bep .="<option  value='".$bep->getBEP_ID()."'>".$bep->getBEP_TEN_BEP()."</option>";
                            
                          }
                    
                }
            }
                    
                    
                    $xtemplate->assign('BEP', $option_bep);
                    
                    
                    
                    
                    $hanghoas = $this->getAllHangHoa();


            if ($hanghoas != null) {
                 
                foreach ($hanghoas as $hanghoa) {
                    
                    $chitietkiemke=$this->getChiTietKiemkeById($kiemke->getKK_ID(),$hanghoa->getID());
                    if($chitietkiemke!=null){
                        $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$chitietkiemke->getCTKK_DON_GIA_NHAP() );
                    $xtemplate->assign('SO_LUONG',$chitietkiemke->getCTKK_SO_LUONG());
                    }else{
                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('SO_LUONG',0);
                    }
                    
                    
                    $xtemplate->parse('main.kiemke.hanghoa');
                    
                }
            }
                    
                  $hanghoas_gia_soluongs = $this->getAllHangHoa();


            if ($hanghoas_gia_soluongs != null) {
                 
                foreach ($hanghoas_gia_soluongs as $hanghoa) {

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',$hanghoa->getDON_GIA_NHAP() );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    
                    
                    
                    
                    $xtemplate->parse('main.kiemke.hanghoas_gia_soluong');
                    
                }
            }  
                    
                    
                    $xtemplate->parse('main.kiemke');
                   
                
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
            
            
        } 
        else {
            $viewFile = $dir . '/views/kiemke.php';
            $xtemplate = new XTemplate($viewFile);
            $kiemkes = $this->getAllKiemke();


            if ($kiemkes != null) {
                foreach ($kiemkes as $kiemke) {

                    $xtemplate->assign('KK_ID', $kiemke->getKK_ID());
                     $xtemplate->assign('KK_NGAY_KIEM_KE_BEGIN',Utility::formatDateTimeToView($kiemke->getKK_NGAY_KIEM_KE_BEGIN()) );
                    $xtemplate->assign('KK_NGAY_KIEM_KE_END',Utility::formatDateTimeToView($kiemke->getKK_NGAY_KIEM_KE_END()) );
                    $xtemplate->assign('BEP', $this->getBepById($kiemke->getBEP_ID())->getBEP_TEN_BEP());
                    $xtemplate->assign('KK_TONG_KHO',Utility::formatToCurrencyString($kiemke->getKK_TONG_KHO()) );
                    $xtemplate->parse('main.kiemke');
                   
                }
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
          
    }

 
     private function getHoaHongById($id,$truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getByID($id,$truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getArrayHoaHongById($truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getArrayByID($truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
   
     private function  getAllLoaiHoaHong() {
        $db = new Database();
        try {
            $loaihoahong = new LoaiHoaHong();

            $result = $loaihoahong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
     
    
    
    
   
    
        private function updateKiemke($KK_ID,$BEP_ID, $KK_NGAY_KIEM_KE_BEGIN,$KK_NGAY_KIEM_KE_END,$KK_TONG_KHO,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
            
                $kiemke = new Kiem_ke();
                $kiemke->setKK_ID($KK_ID);
                $kiemke->setBEP_ID($BEP_ID);
                
                 $date = new DateTime($KK_NGAY_KIEM_KE_BEGIN);
                 $kiemke->setKK_NGAY_KIEM_KE_BEGIN($date->format("Y-m-d"));
                 $kiemke->setKK_NGAY_KIEM_KE_END($KK_NGAY_KIEM_KE_END);
                 $kiemke->setKK_TONG_KHO($KK_TONG_KHO);
                 $kiemke->setIS_ACTIVE(1);
                $kiemke->update($db);

                
                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietkiemke=new Chi_tiet_kiem_ke();
                $chitietkiemke->setKK_ID($kiemke->getKK_ID());
                $chitietkiemke->setHH_ID($ID_HH_[$i]);
                $chitietkiemke->setCTKK_DON_GIA_NHAP($DON_GIA_[$i]);
                $chitietkiemke->setCTKK_SO_LUONG($SOLUONG_[$i]);
                $chitietkiemke->setCTKK_THANH_TIEN($THANHTIEN_[$i]);
                $chitietkiemke->setIS_ACTIVE(1);
                $chitietkiemke->insert($db);
                
            }
                
                
               
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    

    
    
    
   //----------------------đang sử dụng 
    
    
    private function deleteChiTietKiemke($values) {
        $db = new Database();
        try {            
           
                $chitietkiemke = new Chi_tiet_kiem_ke();
                $chitietkiemke->setKK_ID($values);
                $chitietkiemke->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    

    
      private function getChiTietKiemkeById($kiemke,$hanghoa) {
        $db = new Database();
        try {
            $chitietkiemke = new Chi_tiet_kiem_ke();
            $result = $chitietkiemke->getByID($kiemke,$hanghoa,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
       private function getAllKiemke() {
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $result = $kiemke->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
  private function getKiemkeById($id) {
        $db = new Database();
        try {
            $kiemke = new Kiem_ke();
            $kiemke->setKK_ID($id);
            $result = $kiemke->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
      private function getBepById($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
     private function addKiemke($BEP_ID, $KK_NGAY_KIEM_KE_BEGIN,$KK_NGAY_KIEM_KE_END,$KK_TONG_KHO,$DON_GIA,$SOLUONG,$THANHTIEN,$ID_HH){
        
         $db = new Database();
        try {            
           
            
                $kiemke = new Kiem_ke();
                $maxTuan=$kiemke->getMaxID_($db);
                if($maxTuan->getKK_ID() ==''){
                    
                    $kiemke->setTUAN_ID(1);   
                     $kiemke->setTHANG_ID(1);
                }else{
                   $kiemke_max=new Kiem_ke();
                   $kiemke_max->setKK_ID($maxTuan->getKK_ID());
                   $kiemke_maxs=$kiemke_max->getByID($db);
                    if(Utility::getWeekDates($KK_NGAY_KIEM_KE_BEGIN,$kiemke_maxs->getKK_NGAY_KIEM_KE_BEGIN())){
                        
                         $kiemke->setTUAN_ID($kiemke_maxs->getTUAN_ID());   
                         $kiemke->setTHANG_ID($kiemke_maxs->getTHANG_ID());
                          
                    }else{
                       
                        if($kiemke_maxs->getTUAN_ID()<4){//echo $kiemke_maxs->getTUAN_ID();exit();
                             $kiemke->setTUAN_ID($kiemke_maxs->getTUAN_ID()+1);   
                             $kiemke->setTHANG_ID($kiemke_maxs->getTHANG_ID());
                              
                        }else{
                             $kiemke->setTUAN_ID(1);   
                             $kiemke->setTHANG_ID($kiemke_maxs->getTHANG_ID()+1);
                        }
                    }
                    
                }
                

                
                $kiemke->setBEP_ID($BEP_ID);
                
                 $date = new DateTime($KK_NGAY_KIEM_KE_BEGIN);
                 $kiemke->setKK_NGAY_KIEM_KE_BEGIN($date->format("Y-m-d"));
                 $kiemke->setKK_NGAY_KIEM_KE_END($KK_NGAY_KIEM_KE_END);
                 $kiemke->setKK_TONG_KHO($KK_TONG_KHO);
                 $kiemke->setIS_ACTIVE(1);
                  $kiemke->setKK_ID(null);
                $kiemke->insert($db);

                $DON_GIA_ = explode(',', $DON_GIA);
                $SOLUONG_= explode(',', $SOLUONG);
                $THANHTIEN_  = explode(',', $THANHTIEN);     
                $ID_HH_= explode(',', $ID_HH);  
                for ($i = 0; $i < sizeof($ID_HH_); $i++) {
                    
                $chitietkiemke=new Chi_tiet_kiem_ke();
                $chitietkiemke->setKK_ID($kiemke->getKK_ID());
                $chitietkiemke->setHH_ID($ID_HH_[$i]);
                $chitietkiemke->setCTKK_DON_GIA_NHAP($DON_GIA_[$i]);
                $chitietkiemke->setCTKK_SO_LUONG($SOLUONG_[$i]);
                $chitietkiemke->setCTKK_THANH_TIEN($THANHTIEN_[$i]);
                $chitietkiemke->setIS_ACTIVE(1);
                $chitietkiemke->insert($db);
            }
                
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    
    private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getAllHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();

            $result = $hanghoa->getList_($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
        private function deleteKiemke($values) {
        $db = new Database();
        try {            
           
                $kiemke = new Kiem_ke();
                $kiemke->setKK_ID($values);
                $kiemke->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    
    
}
